export default {
  data() {
    return {
      messageThrottle: {
        lastMessageTime: {},
        messageCount: {},
        throttleInterval: 1000, // 1 second between similar messages
        maxMessagesPerType: 3 // Max messages of same type in interval
      }
    }
  },

  methods: {
    // Controlled message display - prevents spam
    showMessage(type, message, key = null) {
      const now = Date.now()
      const messageKey = key || message
      const typeKey = `${type}-${messageKey}`
      
      // Check if we should throttle this message
      const lastTime = this.messageThrottle.lastMessageTime[typeKey] || 0
      const timeDiff = now - lastTime
      
      if (timeDiff < this.messageThrottle.throttleInterval) {
        // Count how many times this message appeared
        this.messageThrottle.messageCount[typeKey] = (this.messageThrottle.messageCount[typeKey] || 0) + 1
        
        // Only show if under limit
        if (this.messageThrottle.messageCount[typeKey] > this.messageThrottle.maxMessagesPerType) {
          return // Skip this message
        }
      } else {
        // Reset count after interval
        this.messageThrottle.messageCount[typeKey] = 1
      }
      
      this.messageThrottle.lastMessageTime[typeKey] = now
      
      // Show the message
      this.$message({
        type,
        message,
        duration: type === 'error' ? 4000 : 2500,
        showClose: true
      })
    },

    // Convenience methods
    showSuccess(message, key = null) {
      this.showMessage('success', message, key)
    },

    showError(message, key = null) {
      this.showMessage('error', message, key)
    },

    showInfo(message, key = null) {
      this.showMessage('info', message, key)
    },

    showWarning(message, key = null) {
      this.showMessage('warning', message, key)
    },

    // Batch message display - shows summary instead of individual messages
    showBatchMessage(items, messageTemplate, type = 'info') {
      if (items.length === 0) return
      
      if (items.length === 1) {
        this.showMessage(type, messageTemplate(items[0]))
      } else {
        // Show summary message
        const summary = `${items.length} ${this.$t('common.items')}`
        this.showMessage(type, summary)
      }
    }
  }
}